package cn.jobstart.service.impl;

import cn.jobstart.common.*;
import cn.jobstart.dao.IAdminJobDao;
import cn.jobstart.es.search.ResumeEsSearch;
import cn.jobstart.es.service.IResumeEsService;
import cn.jobstart.model.AdminJobManager;
import cn.jobstart.modelExt.AdminExt;
import cn.jobstart.modelExt.AdminJobExt;
import cn.jobstart.modelExt.AdminJobManagerExt;
import cn.jobstart.pubInter.AbstractBaseService;
import cn.jobstart.pubInter.IBaseDao;
import cn.jobstart.pubInter.exception.SkException;
import cn.jobstart.searchExt.AdminJobSearchExt;
import cn.jobstart.service.IAdminJobCandidateService;
import cn.jobstart.service.IAdminJobManagerService;
import cn.jobstart.service.IAdminJobService;
import cn.jobstart.service.IAdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * @author sunke
 * @Date 2023-09-06 15:00:20
 * @description AdminJobService      员工职位
 */
@Service(value = "adminJobService")
@Slf4j
public class AdminJobServiceImpl extends AbstractBaseService<AdminJobExt, AdminJobSearchExt, String> implements IAdminJobService {

    private static final byte[] LOCK = new byte[0];
    @Resource(name = "adminJobDao")
    private IAdminJobDao adminJobDao;
    @Resource
    private IResumeEsService resumeEsService;

    @Resource
    private IAdminJobManagerService adminJobManagerService;


    @Resource
    private IAdminJobCandidateService adminJobCandidateService;

    @Resource
    private IAdminService adminService;


    @Override
    public IBaseDao<AdminJobExt, AdminJobSearchExt, String> getDao() {
        return adminJobDao;
    }


    /**
     * 修改职位负责人ID
     *
     * @param adminJobId
     * @param adminIds
     * @param modAdminId
     * @param modAdminTruename
     * @throws SkException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modManager(String adminJobId, List<String> adminIds, String modAdminId, String modAdminTruename) throws SkException {
        if (StringUtil.isNullOrEmpty(adminJobId)) {
            return;
        }
        if (adminIds == null) {
            return;
        }

        AdminJobExt adminJob = getDetail(adminJobId, new HashSet<>());

        adminJobManagerService.clearByAdminJobId(adminJobId);

        List<AdminExt> adminList = adminService.getAllByIds(adminIds, new HashSet<>());

        List<AdminJobManagerExt> adminJobManagerList = SkList.getInstance();


        adminList.stream().forEach(item -> {

            AdminJobManagerExt adminJobManager = new AdminJobManagerExt();

            adminJobManager.setAdminId(item.getId());
            adminJobManager.setAdminTruename(item.getTruename());
            adminJobManager.setAdminJobId(adminJobId);
            adminJobManager.setAdminJobTitle(adminJob.getTitle());
            adminJobManagerList.add(adminJobManager);
        });
        if (StringUtil.isNotObjEmpty(adminJobManagerList)) {

            adminJobManagerService.batchInsert(adminJobManagerList);
        }


    }

    @Override
    public void fullOtherData(List<AdminJobExt> list, Set<String> fullConfigSet) {
        super.fullOtherData(list, fullConfigSet);
        if (StringUtil.isNullOrEmpty(list)) {
            return;
        }
        fullManager(list, fullConfigSet);
        // fullCandidate(list,fullConfigSet);
    }



/*    private void fullCandidate(List<AdminJobExt> list, Set<String> fullConfigSet){

        try{
            if(!FullConfigKeyConst.hasFullData(fullConfigSet,FullConfigKeyConst.ADMIN_JOB_CANDIDATE_FULL_LIST_KEY)){
                return;
            }
            List<String> ids=list.stream().map(AdminJobExt::getId).distinct().collect(Collectors.toList());

            List<AdminJobCandidateExt> candidateList=adminJobCandidateService.getAllByInField(ids, AdminJobCandidate.ADMIN_JOB_ID,fullConfigSet);
            if(StringUtil.isNullOrEmpty(candidateList)){
                return;
            }
            Map<String,List<AdminJobCandidateExt>> dataMap=candidateList.stream().collect(Collectors.groupingBy(AdminJobCandidate::getAdminJobId));
            list.stream().forEach(item->{
                try{
                    item.setCandidateList(dataMap.get(item.getId()));
                }catch (Exception e){
                }
            });



        }catch (Exception e){

        }

    }*/


    private void fullManager(List<AdminJobExt> list, Set<String> fullConfigSet) {
        try {
            if (!FullConfigKeyConst.hasFullData(fullConfigSet, FullConfigKeyConst.ADMIN_JOB_MANAGER_FULL_LIST_KEY)) {
                return;
            }

            List<String> ids = list.stream().map(AdminJobExt::getId).distinct().collect(Collectors.toList());

            List<AdminJobManagerExt> managerList = adminJobManagerService.getAllByInField(ids, AdminJobManager.ADMIN_JOB_ID, fullConfigSet);
            if (StringUtil.isNullOrEmpty(managerList)) {
                return;
            }
            Map<String, List<AdminJobManagerExt>> dataMap = managerList.stream().collect(Collectors.groupingBy(AdminJobManagerExt::getAdminJobId));
            list.stream().forEach(item -> {
                try {
                    item.setManagerList(dataMap.get(item.getId()));
                } catch (Exception e) {
                }
            });


        } catch (SkException e) {

        }


    }

    @Override
    public void afterInsert(AdminJobExt obj) throws SkException {
        super.afterInsert(obj);
        AdminJobManagerExt adminJobManager = new AdminJobManagerExt();
        adminJobManager.setAdminJobId(obj.getId());
        adminJobManager.setAdminJobTitle(obj.getTitle());
        adminJobManager.setAdminId(obj.getAdminId());
        adminJobManager.setAdminTruename(obj.getAdminTruename());
        adminJobManagerService.insert(adminJobManager);
    }

    /**
     * 生成编号
     *
     * @return
     */
    @Override
    public String createNumberCode() {
        AdminJobSearchExt adminJobSearch = AdminJobSearchExt.getInstance();
        int total = getCountBySearch(adminJobSearch);

        while (true) {
            total = total + 1;
            String numberCode = "job" + GetCode.intToStr(total, 6);
            if (StringUtil.isNullOrEmpty(getDetailByNumberCode(numberCode))) {
                return numberCode;
            }

        }


    }

    @Override
    public void beforeInsert(AdminJobExt obj) throws SkException {
        setResumeTotal(obj);
        super.beforeInsert(obj);
    }

    private void setResumeTotal(AdminJobExt obj) throws SkException {
        ResumeEsSearch search = ResumeEsSearch.getInstance();
        search.setKeywordLike(obj.getTitle());
        obj.setResumeTotal(resumeEsService.getCountBySearch(search));
    }

    @Override
    public void beforeUpdate(AdminJobExt obj) throws SkException {

        setResumeTotal(obj);

        super.beforeUpdate(obj);
    }

    @Override
    public AdminJobExt insert(AdminJobExt obj) throws SkException {
        synchronized (LOCK) {
            obj.setNumberCode(createNumberCode());
            return super.insert(obj);
        }
    }

    /**
     * 查询
     *
     * @param numberCode
     * @return
     */
    @Override
    public AdminJobExt getDetailByNumberCode(String numberCode) {
        if (StringUtil.isNullOrEmpty(numberCode)) {
            return null;
        }

        return getDetailBySearch(AdminJobSearchExt.getInstance().setNumberCodeEq(numberCode));
    }

    /**
     * 更新职位候选人数量
     *
     * @param adminJobId
     * @param count
     */
    @Override
    public void updateCandidateTotalByAdminJobIdAndCount(String adminJobId, int count) {
        if (StringUtil.isNullOrEmpty(adminJobId) || count == 0) {
            return;
        }

        try {
            AdminJobExt adminJob = getDetail(adminJobId, new HashSet<>());
            adminJob.setCandidateTotal(NumberUtil.parseInt(adminJob.getCandidateTotal()) + count);
            update(adminJob);

        } catch (SkException e) {

        }


    }
}